package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/7/11 22:19
 * @description：https://leetcode-cn.com/problems/add-strings/
 */
public class 字符串相加 {
    @Test
    public void test() {
        Solution solution = new Solution();
    }

    class Solution {
        public String addStrings(String num1, String num2) {
            StringBuilder builder1 = new StringBuilder(num1);
            StringBuilder builder2 = new StringBuilder(num2);
            builder1.reverse();
            builder2.reverse();
            StringBuilder ans = new StringBuilder();
            int i = 0;
            int j = 0;
            int factor = 0;
            while (i < builder1.length() || j < builder2.length()) {
                int a = i < builder1.length() ? (builder1.charAt(i++) - '0') : 0;
                int b = j < builder2.length() ? (builder2.charAt(j++) - '0') : 0;
                ans.append((a + b + factor) % 10);
                factor = (a + b + factor) / 10;
            }
            if (factor != 0) {
                ans.append(factor);
            }
            return ans.reverse().toString();
        }
    }

}
